package com.wanmait.hotelManage.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wanmait.hotelManage.util.DBHelper;
import com.wanmait.hotelManage.vo.RoomType;
import com.wanmait.hotelManage.vo.Roomimages;

public class RoomimagesDAO {
	private RoomimagesDAO() {};
	private static RoomimagesDAO roomimagesdao=new RoomimagesDAO();
	public static RoomimagesDAO getRoomimagesdao() {
		return roomimagesdao;
	}
	//通过roomtypeid查找image
	public List<Roomimages> findImagesByRoomTypeId(RoomType roomtype) {
		// TODO Auto-generated method stub
		List<Roomimages> roomimages=new ArrayList<Roomimages>();
		Roomimages roomimage=null;
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		
		String sql="select id,pic from room_images where room_type_id=?";
		
		try {
			conn=DBHelper.getDBHelper().getConn();
			ps=conn.prepareStatement(sql);
			ps.setInt(1,roomtype.getId());
			rs=ps.executeQuery();
			
			while(rs.next()) {
				roomimage=new Roomimages();
				roomimage.setId(rs.getInt("id"));
				roomimage.setPic(rs.getString("pic"));
				roomimage.setRoomtype(roomtype);
				roomimages.add(roomimage);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBHelper.getDBHelper().close(rs, ps, conn);
		}
		
		return roomimages;
	}
	//通过roomtypeid删除
	public void deleteByRoomTypeId(Integer id) {
		// TODO Auto-generated method stub
		String sql="DELETE FROM room_images WHERE room_type_id=?";
		
		DBHelper.getDBHelper().executeUpdate(sql, id);
		
	}
	//通过id更新pic
	public void updatePicById(String fileName,int id) {
		// TODO Auto-generated method stub
		String sql="update room_images set pic=? where id=?";
		DBHelper.getDBHelper().executeUpdate(sql, fileName,id);
	}
	//通过roomtypeid添加images
	public void insertImagesByRoomTypeId(String fileName, int roomTypeId) {
		// TODO Auto-generated method stub
		String sql="insert into room_images(room_type_id,pic) values(?,?)";
		DBHelper.getDBHelper().executeUpdate(sql, roomTypeId,fileName);	
	}	
	public static void main(String[] args) {
		RoomType roomtype=new RoomType();
		roomtype.setId(1);
		List<Roomimages>s=RoomimagesDAO.getRoomimagesdao().findImagesByRoomTypeId(roomtype);
		for (Roomimages roomimages : s) {
			System.out.println(roomimages);
		}
	}
}
